<template>
  <div class="biglogin">
    <div class="login">
      <img class="login-bimg" src="./../../assets/login.png" alt="">
      <div class="login-fr">
        <img class="login-simg" src="./../../assets/loginlogo.png" alt="">
        <el-form ref="login" :model="user" :rules="rules" label-width="120px" class="demo-ruleForm" :size="formSize"
          status-icon>
          <el-form-item label="用户名" prop="username">
            <el-input v-model="user.username" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="user.password" type="password" />
          </el-form-item>
          <!-- <el-form-item label="选择登录角色">
            <el-radio-group>
              <el-radio label="管理员登录" />
              <el-radio label="普通用户登录" />
            </el-radio-group>
          </el-form-item> -->
          <el-form-item>
            <el-button type="primary" @click="doLogin">登录</el-button>
            <el-button @click="restForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>

</template>

<script>
import { userLogin } from '@/api/login'

export default {
  name: 'LaginPage',
  data() {
    return {
      user: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '必填' },
          { pattern: /^[a-zA-Z]\w{3,7}$/, message: '用户名必须由4-8位数字字母下滑线组成,必须以字母开头' }
        ],
        password: [
          { required: true, message: '必填' }
        ]
      }
    }
  },
  methods: {
    doLogin() {
      this.$refs.login.validate(valid => {
        if (valid) {
          userLogin(this.user).then(res => {
            if (res.data.code === 200) {
              localStorage.setItem('user', JSON.stringify(res.data.data))
              localStorage.setItem('token', res.data.data.token)
              localStorage.setItem('role', res.data.data.role)
              this.$store.commit('user/INIT_USER', res.data.data)
              // console.log(res)
              this.$message({
                type: 'success',
                message: '用户登录成功',
                duration: 1500,
                onClose: () => {
                  // 跳转到 主页
                  this.$router.replace('/dashBoard')
                }
              })
            } else {
              this.$message({
                type: 'error',
                message: '登录失败'
              })
            }
          })
        } else {
          alert('验证失败')
        }
      })
    },
    restForm() {
      this.$refs.login.resetFields()
    }
  }
}

</script>

<style lang="scss" scoped>
.biglogin {
  background: radial-gradient(ellipse at bottom, #5db8ff 0, #0076d1 100%);
  display: flex;
  width: 100vw;
  height: 100vh;

  .login {
    width: 1000px;
    height: 454px;
    background-color: #fff;
    margin: auto;
    border-radius: 10px;
    box-shadow: 0px 0px 10px 5px #999;
    padding: 20px;
    display: flex;

    .login-bimg {
      width: 400px;
      height: 400px;
      margin: 0 40px;
    }

    .login-fr {
      width: 480px;
      margin: 40px;

      .login-simg {
        height: 50px;
        display: block;
        margin: 0 auto;
        margin-bottom: 100px;
      }
    }

  }
}
</style>
